(function(window, document) {
    angular
        .module('app')
        .factory('uidFactory', uidFactory);

    uidFactory.$inject = [
        'WebViewJavascriptBridgeFactory',
        'userAgentFactory',
        '$cookies'
    ];

    function uidFactory (WebViewJavascriptBridgeFactory, userAgentFactory,$cookies) {
        var service = {};
        var _uid;
        // $cookies.remove('vipbanlvUid');
        service.getUid = function() {
            var dataForAndroid;
            _uid = $cookies.get('vipbanlvUid');
            if (_uid === null || _uid === undefined) {
                // ios 环境下使用通信框架
                if (userAgentFactory.getOS() == 'ios') {
                    WebViewJavascriptBridgeFactory.init(function(bridge) {
                        bridge.callHandler('vipbanlvUidCall', {}, function(data) {
                            $cookies.put('vipbanlvUid', data.vipbanlvUid);
                            _uid = data.vipbanlvUid;
                        });
                    });

                // android 环境下使用原生js注入
                } else if (userAgentFactory.getOS() == 'android') {

                    // alert('是android系统哟!');
                    // 判断是否存在注入的js对象
                    if (typeof vipbanlvFunc == 'object') {
                        // alert('检测到注入的对象了!');
                        // 调用注入的js对象的方法
                        dataForAndroid = vipbanlvFunc.vipbanlvUidCall();
                        // alert('返回值是: ' + dataForAndroid);
                        // json 字符串转js对象
                        if (angular.isString(dataForAndroid)) {
                            dataForAndroid = angular.fromJson(dataForAndroid);
                        }

                        // 存cookies
                        $cookies.put('vipbanlvUid', dataForAndroid.vipbanlvUid);
                        // 设置返回值
                        _uid = dataForAndroid.vipbanlvUid;
                    }
                }
            }

            return _uid;
        };

        return service;
    }
})(window, document);
